﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BusinessLogicTier.Connection;
using System.Data.SqlClient;
using Core;
namespace QLBH
{
    public partial class FrmConnection : DevExpress.XtraEditors.XtraForm
    {
        Utils util = new Utils();
        public static string sKeyEncripts = "465897234562394857623498"; //Key mã hóa - chuỗi 24 kỹ tự
        public FrmConnection()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (grAuthentication.SelectedIndex == 0)
            {
                string server = util.EncryptCode(txtserver.EditValue.ToString(), sKeyEncripts, false);
                string database = util.EncryptCode(cmbdatabase.Text, sKeyEncripts, false);
                BusinessLogicTier.Connection.Utilities.XML.XMLWriter("Connection.xml", server, database, "true");
            }
            else
            {
                string server = util.EncryptCode(txtserver.EditValue.ToString(), sKeyEncripts, false);
                string username = util.EncryptCode(txtusername.EditValue.ToString(), sKeyEncripts, false);
                string matkhau = util.EncryptCode(txtmatkhau.EditValue.ToString(), sKeyEncripts, false);
                string database = util.EncryptCode(cmbdatabase.Text, sKeyEncripts, false);
                BusinessLogicTier.Connection.Utilities.XML.XMLWriter("Connection.xml", server, username, matkhau, database, "false");
            }

            this.DialogResult = DialogResult.OK;
        }

        private void btnTesconnection_Click(object sender, EventArgs e)
        {
            if (grAuthentication.SelectedIndex == 0)
            {
                cmbdatabase.Items.Clear();
                string server1 = util.EncryptCode(txtserver.ToString(), sKeyEncripts, false);
                string Userver = util.DecryptCode(server1, sKeyEncripts, false);
                SqlConnection m_Connect = new SqlConnection("Data Source=" + Userver + ";Initial Catalog=master;Integrated Security=True;");
                SqlCommand m_Cmd = new SqlCommand("SP_DATABASES", m_Connect);
                SqlDataReader m_DReader;
                try
                {
                    m_Connect.Open();
                    m_DReader = m_Cmd.ExecuteReader();
                    while (m_DReader.Read())
                    {
                        cmbdatabase.Items.Add(m_DReader[0].ToString());
                    }
                    MessageBox.Show("Kết nối thành công!", "Succecced", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (SqlException sqlEx)
                {
                    MessageBox.Show(sqlEx.Message, "FAILED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    if (m_Connect.State == ConnectionState.Open)
                        m_Connect.Close();

                    m_Connect.Dispose();
                    m_Cmd.Dispose();
                }
            }
            if (grAuthentication.SelectedIndex == 1)
            {
                cmbdatabase.Items.Clear();
                string server1 = util.EncryptCode(txtserver.EditValue.ToString(), sKeyEncripts, false);
                string username1 = util.EncryptCode(txtusername.EditValue.ToString(), sKeyEncripts, false);
                string matkhau1 = util.EncryptCode(txtmatkhau.EditValue.ToString(), sKeyEncripts, false);

                string Userver = util.DecryptCode(server1, sKeyEncripts, false);
                string Uusername = util.DecryptCode(username1, sKeyEncripts, false);
                string Umatkhau = util.DecryptCode(matkhau1, sKeyEncripts, false);
                SqlConnection m_Conn = new SqlConnection("Data Source=" + Userver + ";Initial Catalog=master;User Id=" + Uusername + ";Password=" + Umatkhau + ";");
                SqlCommand m_Cmd = new SqlCommand("SP_DATABASES", m_Conn);
                SqlDataReader m_DReader;

                try
                {
                    m_Conn.Open();
                    m_DReader = m_Cmd.ExecuteReader();
                    while (m_DReader.Read())
                    {
                        cmbdatabase.Items.Add(m_DReader[0].ToString());
                    }
                    MessageBox.Show("Kết nối thành công!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (SqlException sqlEx)
                {
                    MessageBox.Show(sqlEx.Message, "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    if (m_Conn.State == ConnectionState.Open)
                        m_Conn.Close();

                    m_Conn.Dispose();
                    m_Cmd.Dispose();
                }
            }
        }

        private void grAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (grAuthentication.SelectedIndex == 0)
            {
                txtusername.Enabled = false;
                txtmatkhau.Enabled = false;
            }
            else
            {
                txtusername.Enabled = true;
                txtmatkhau.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}